package com.acce.gxsc.page.inCarDetail.editInCarDetail;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.acce.gxsc.R;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.netBean.InCarBean;
import com.acce.gxsc.model.netBean.InCarGoodsBean;
import com.acce.gxsc.model.netBean.InGoodsBean;
import com.acce.gxsc.page.registerCar.addBoxInfo.AddBoxInfoActivity;
import com.acce.gxsc.page.registerCar.addGoodsInfo.AddGoodsInfoActivity;
import com.acce.sdk.base.mvp.BaseMvpActivity;
import com.acce.sdk.utils.LogUtils;

import java.text.MessageFormat;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditInCarDetailActivity extends BaseMvpActivity<EditInCarDetailView, EditInCarDetailPresenter> implements EditInCarDetailView {

    public static final int CODE_ADD_CAR_INFO    = 1001;
    public static final int CODE_ADD_CUSTOM_INFO = 1002;
    public static final int CODE_ADD_GOODS_INFO  = 1003;
    public static final int CODE_ADD_BOX_INFO    = 1004;
    public static final int CODE_EDIT_COMPANY    = 1005;
    public static final int CODE_REGISTER_CAR    = 1006;

    LayoutInflater mInflater;
    @Bind(R.id.tv_order_id)
    TextView mTvOrderId;
    @Bind(R.id.tv_father)
    TextView mTvFather;
    @Bind(R.id.tv_son)
    TextView mTvSon;
    @Bind(R.id.tv_customer)
    EditText mTvCustomer;
    @Bind(R.id.tv_car_info)
    EditText mTvCarInfo;
    @Bind(R.id.tv_box_info)
    TextView mTvBoxInfo;
    @Bind(R.id.ll_goods_list)
    LinearLayout mLlGoodsList;
    @Bind(R.id.tv_goods_info)
    TextView mTvGoodsInfo;
    @Bind(R.id.tv_add_goods)
    TextView tvAddGoods;
    @Bind(R.id.tv_clear_allGoods)
    TextView tvClearGoods;

    InCarBean inCarInfo;
    ArrayList<InCarGoodsBean> inGoodsInfo;

    @Bind(R.id.tv_acce_temp_1)
    TextView mTvAcceTemp1;

    @Override
    protected EditInCarDetailPresenter createPresenter() {
        return new EditInCarDetailPresenter();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_in_car_detail);
        ButterKnife.bind(this);


        initView();
    }

    private void initView() {
        mInflater = LayoutInflater.from(this);
        Intent intent = getIntent();
        if (intent != null) {
            inCarInfo = (InCarBean) intent.getSerializableExtra("inCarInfo");
            inGoodsInfo = (ArrayList<InCarGoodsBean>) intent.getSerializableExtra("inGoodsInfo");
        }
        Tools.setText(mTvOrderId, inCarInfo.inCarId);
        Tools.setText(mTvFather, inCarInfo.underCompany);
        Tools.setText(mTvSon, inCarInfo.consignor);
        Tools.setText(mTvCustomer, inCarInfo.contactName);
        Tools.setText(mTvBoxInfo, inCarInfo.containNo);
        mTvCarInfo.setText(Tools.getString1(inCarInfo.plateNumX, inCarInfo.plateNum, inCarInfo.hangNum));
        mTvAcceTemp1.setText(Tools.getString(inCarInfo.acceTemp1));
        Tools.setText(mTvGoodsInfo, MessageFormat.format("-{0}-项", inGoodsInfo == null ? 0 : inGoodsInfo.size()));
        mLlGoodsList.removeAllViews();
        if (inGoodsInfo != null) {
            for (int i = 0; i < inGoodsInfo.size(); i++) {
                InCarGoodsBean goodsBean = inGoodsInfo.get(i);
                View childView = mInflater.inflate(R.layout.child_goods, mLlGoodsList, false);
                TextView tvGoodsId = (TextView) childView.findViewById(R.id.tv_goods_id);
                TextView tvGoodsType = (TextView) childView.findViewById(R.id.tv_goods_type);
                TextView tvGoodsName = (TextView) childView.findViewById(R.id.tv_goods_name);
                TextView tvGoodsPackaging = (TextView) childView.findViewById(R.id.tv_packaging);
                TextView tvGoodsNum1 = (TextView) childView.findViewById(R.id.tv_goods_num_1);
                TextView tvGoodsNum2 = (TextView) childView.findViewById(R.id.tv_goods_num_2);
                TextView tvWeight = (TextView) childView.findViewById(R.id.tv_weight);
                TextView tvGoodsRemarks= (TextView) childView.findViewById(R.id.input_goods_remarks);
// TextView tvTotalWeight = (TextView) childView.findViewById(R.id.tv_total_weight);
                tvGoodsId.setText(String.valueOf(i + 1));
                tvGoodsType.setText(goodsBean.goodsType);
                tvGoodsName.setText(goodsBean.goodsName);
                tvGoodsPackaging.setText(goodsBean.containType);
                tvGoodsNum1.setText(goodsBean.plateAmount);
                tvGoodsNum2.setText(goodsBean.amount);
                tvWeight.setText(goodsBean.itemWeight);
                tvGoodsRemarks.setText(Tools.getString(goodsBean.remarks));
//                tvTotalWeight.setText(goodsBean.suttle);
                mLlGoodsList.addView(childView);
            }
        }
    }
    @OnClick({R.id.tv_box_info,R.id.tv_add_goods,R.id.tv_clear_allGoods,R.id.btn_cancel,R.id.btn_sure})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.tv_box_info:
                /*
                Intent boxInfoIntent=new Intent(this, AddBoxInfoActivity.class);
                this.startActivityForResult(boxInfoIntent, CODE_ADD_BOX_INFO);*/
                break;
            case R.id.tv_add_goods:
                startActivityForResult(new Intent(this, AddGoodsInfoActivity.class), CODE_ADD_GOODS_INFO);
                break;
            case R.id.tv_clear_allGoods:
                inGoodsInfo.clear();
                initView();
                break;
            case R.id.btn_cancel:
                finish();
                break;
            case R.id.btn_sure:
                String tvCustomer=mTvCustomer.getText().toString();
                String tvCarInfo=mTvCarInfo.getText().toString();
                LogUtils.d(inCarInfo.inCarKey);
                LogUtils.d(tvCarInfo);
                LogUtils.d(inCarInfo.containNo);
                LogUtils.d(tvCustomer);
                mPresenter.editCar(inCarInfo.inCarKey,tvCarInfo,inCarInfo.containNo,tvCustomer);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(requestCode==CODE_ADD_BOX_INFO){

            }else if (requestCode==CODE_ADD_GOODS_INFO){
                if(resultCode==RESULT_OK&&data!=null){
                    InCarGoodsBean goodsBean=new InCarGoodsBean();
                    goodsBean.goodsId = data.getStringExtra("goodsId");
                    goodsBean.goodsType = data.getStringExtra("goodsType");
                    goodsBean.goodsName = data.getStringExtra("goodsName");
                    goodsBean.containType = data.getStringExtra("goodsPackaging");
                    goodsBean.plateAmount = data.getStringExtra("goodNum1");
                    goodsBean.amount = data.getStringExtra("goodNum2");
                    goodsBean.itemWeight = data.getStringExtra("weight");
                //    goodsBean.suttle = data.getStringExtra("totalWeight");
                //    goodsBean.wunit = data.getStringExtra("kg");
                    goodsBean.remarks = data.getStringExtra("explain");
                    inGoodsInfo.add(goodsBean);
                    initView();
                }
            }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}
